<!--
 * @Description: 商家销量统计的横向柱状图
 * @Author: 梁萌
 * @Date: 2020-11-28 11:28:22
 * @LastEditTime: 2020-12-19 16:42:06
 * @FilePath: \vision\src\components\Seller.vue
-->
<template>
  <div class="com-container">
    <div class="com-chart" ref="seller_ref"></div>
  </div>
</template>

<script>
import { mapState } from "vuex";
export default {
  data() {
    return {
      chartIntance: null,
      allData: null, //服务器返回的数据
      currentPage: 1, //当前页面
      totalPage: 0, //一共多少页
      timerId: null, //翻页定时器标识
    };
  },
  created() {
    //组件创建完成后进行回调函数的注册
    this.$socket.registerCallBack("sellerData", this.getData);
  },
  mounted() {
    this.initChart();
    //this.getData();
    //发送数据给服务器,获取数据
    this.$socket.send({
      action: "getData",
      socketType: "sellerData",
      chartName: "seller",
      value: "",
    });
    window.addEventListener("resize", this.screenAdapter);
    this.screenAdapter();
  },
  destroyed() {
    clearInterval(this.timerId);
    window.removeEventListener("resize", this.screenAdapter);
    //在组件销毁时进行回调函数取消
    this.$socket.unRegisterCallBack("sellerData");
  },
  methods: {
    //初始化echartsInstance对象
    initChart() {
      this.chartIntance = this.$echarts.init(this.$refs.seller_ref, this.theme); //初始化echarts实例对象
      //对图表初始化配置的控制
      const initOption = {
        title: {
          text: "▎商家销售统计",
          left: 20,
          top: 20,
        },
        grid: {
          top: "20%",
          left: "3%",
          right: "6%",
          bottom: "3%",
          containLabel: true,
        },
        xAxis: {
          type: "value",
        },
        yAxis: {
          type: "category",
        },
        tooltip: {
          trigger: "axis", //鼠标放在柱状图条目上时显示文字信息
          //鼠标放在柱状图条目上时显示背景灰色
          axisPointer: {
            type: "line",
            z: 0,
            lineStyle: {
              width: 66,
              color: "#2D3443",
            },
          },
        },
        series: [
          {
            type: "bar",
            label: {
              show: true,
              position: "right",
              textStyle: {
                color: "white",
              },
            },
            itemStyle: {
              //柱状图条目颜色渐变
              color: new this.$echarts.graphic.LinearGradient(0, 0, 1, 0, [
                {
                  offset: 0,
                  color: "#5052EE",
                },
                {
                  offset: 1,
                  color: "#ab6EE5",
                },
              ]),
            },
          },
        ],
      };
      this.chartIntance.setOption(initOption);
      //对图表对象进行数据事件监听
      this.chartIntance.on("mouseover", () => {
        clearInterval(this.timerId);
      });

      this.chartIntance.on("mouseout", () => {
        this.startInterval();
      });
    },

    //获取服务器数据
    getData(ret) {
      //const { data: ret } = await this.$http.get("seller");
      this.allData = ret;
      this.allData.sort((a, b) => {
        return a.value - b.value; //从小到大排序
      });

      //计算数据总页数(一页5条数据)
      this.totalPage =
        this.allData.length % 5 === 0
          ? this.allData.length / 5
          : this.allData.length / 5 + 1;

      this.updateChart();
      this.startInterval(); //启动定时器
    },

    //更新图表
    updateChart() {
      const start = (this.currentPage - 1) * 5;
      const end = this.currentPage * 5;
      const showData = this.allData.slice(start, end);
      const sellerNames = showData.map((item) => {
        return item.name;
      });

      const sellerValues = showData.map((item) => {
        return item.value;
      });

      const dataOption = {
        yAxis: {
          data: sellerNames,
        },
        series: [
          {
            data: sellerValues,
          },
        ],
      };

      this.chartIntance.setOption(dataOption);
    },

    //定时翻页
    startInterval() {
      if (this.timerId) {
        clearInterval(this.timerId);
      }

      this.timerId = setInterval(() => {
        this.currentPage++;
        if (this.currentPage > this.totalPage) {
          this.currentPage = 1;
        }
        this.updateChart();
      }, 3000);
    },

    //当浏览器大小变化时进行屏幕适配
    screenAdapter() {
      const titleFontSize = (this.$refs.seller_ref.offsetWidth / 100) * 3.6;
      //和分辨率大小相关配置
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize,
          },
        },
        tooltip: {
          //鼠标放在柱状图条目上时显示背景灰色
          axisPointer: {
            lineStyle: {
              width: titleFontSize,
            },
          },
        },
        series: [
          {
            barWidth: titleFontSize,
            itemStyle: {
              barBorderRadius: [0, titleFontSize / 2, titleFontSize / 2, 0], //柱状图条目圆角
            },
          },
        ],
      };

      this.chartIntance.setOption(adapterOption);
      this.chartIntance.resize();
    },
  },
  computed: {
    ...mapState(["theme"]),
  },
  watch: {
    theme() {
      this.chartIntance.dispose();
      this.initChart();
      this.screenAdapter();
      this.updateChart();
    },
  },
};
</script>

<style>
</style>